package edu.nf.takeoutdemo.web.user;

import edu.nf.takeoutdemo.commons.base.BaseController;
import edu.nf.takeoutdemo.commons.result.PageResult;
import edu.nf.takeoutdemo.commons.result.Result;
import edu.nf.takeoutdemo.model.dto.OrderCancelDTO;
import edu.nf.takeoutdemo.model.dto.OrderSubmitDTO;
import edu.nf.takeoutdemo.model.dto.OrdersPaymentDTO;
import edu.nf.takeoutdemo.model.vo.OrderPaymentVO;
import edu.nf.takeoutdemo.model.vo.OrderSubmitVO;
import edu.nf.takeoutdemo.model.vo.OrderVO;
import edu.nf.takeoutdemo.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author zjl
 * @date 2025/10/30
 */
@RestController("userOrderController")
@RequestMapping("/user/order")
@RequiredArgsConstructor
@Slf4j
public class OrderController extends BaseController {

    private final OrderService orderService;

    /**
     * 下单
     * @param orderSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrderSubmitDTO orderSubmitDTO){
        log.info("下单订单信息：{}", orderSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(orderSubmitDTO);
        return success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    public Result<?> reminder(@PathVariable("id") Long id){
        orderService.reminder(id);
        return success();
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> getOrderDetail(@PathVariable("id") Long id){
        OrderVO orderVO =  orderService.getDetails(id);
        return success(orderVO);
    }

    @GetMapping("/historyOrders")
    public Result<PageResult> page(int pageNum, int pageSize,Integer payStatus){
        log.info("pageNum={}, pageSize={}, status={}", pageNum, pageSize, payStatus);
        PageResult pageResult = orderService.pageQueryUser(pageNum,pageSize,payStatus);
        return success(pageResult);
    }

    @PostMapping("/repetition/{id}")
    public Result<?> repetition(@PathVariable("id") Long id){
        orderService.repetition(id);
        return success();
    }

    @PutMapping("/cancel/{id}")
    public Result<?> cancelOrder(@PathVariable("id")  Long id) throws Exception {
        orderService.cancelUserOrder(id);
        return success();
    }
}
